// 支付状态管理
import { defineStore } from 'pinia'

export interface OrderInfo {
  id: string
  deviceId: string
  deviceName: string
  amount: number
  status: 'pending' | 'paid' | 'cancelled' | 'refunded'
  createdAt: string
  paidAt?: string
}

interface PaymentState {
  orders: OrderInfo[]
  currentOrder: OrderInfo | null
}

export const usePaymentStore = defineStore('payment', {
  state: (): PaymentState => ({
    orders: [],
    currentOrder: null
  }),
  
  getters: {
    getOrders: (state: PaymentState) => state.orders,
    getCurrentOrder: (state: PaymentState) => state.currentOrder,
    getOrderById: (state: PaymentState) => (orderId: string) => 
      state.orders.find(order => order.id === orderId)
  },
  
  actions: {
    // 创建订单
    createOrder(order: OrderInfo) {
      this.orders.push(order)
      this.currentOrder = order
    },
    
    // 更新订单状态
    updateOrderStatus(orderId: string, status: OrderInfo['status'], paidAt?: string) {
      const order = this.orders.find(o => o.id === orderId)
      if (order) {
        order.status = status
        if (paidAt) {
          order.paidAt = paidAt
        }
      }
      
      if (this.currentOrder && this.currentOrder.id === orderId) {
        this.currentOrder.status = status
        if (paidAt) {
          this.currentOrder.paidAt = paidAt
        }
      }
    },
    
    // 设置当前订单
    setCurrentOrder(order: OrderInfo) {
      this.currentOrder = order
    },
    
    // 清除当前订单
    clearCurrentOrder() {
      this.currentOrder = null
    }
  }
})